package edu.ccut.saturn.studio.menuitem.dialog;

import java.util.Arrays;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;

public class FilterDialog extends AbstractElementListSelectionDialog {
	private String[][] res;
	private Label displayLabel;

	public FilterDialog(Shell parent, ILabelProvider renderer, String[][] str) {
		super(parent, renderer);
		this.res = str;
	}

	protected void computeResult() {
		setResult(Arrays.asList(getSelectedElements()));
	}

	protected Control createDialogArea(Composite parent) {
		Composite contents = (Composite) super.createDialogArea(parent);
		createSearch(contents);
		createFilterText(contents);
		createFilteredList(contents);
		createMessageArea(contents);
		setListElements(res[0]);
		setSelection(getInitialElementSelections().toArray());
		dialogChanged(res[0]);

		return contents;
	}

	protected Text createJarPathText(Composite parent) {

		Text text = new Text(parent, SWT.BORDER | SWT.READ_ONLY);

		GridData data = new GridData();
		data.grabExcessVerticalSpace = false;
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.BEGINNING;
		text.setLayoutData(data);
		text.setFont(parent.getFont());

		return text;

	}

	private Composite createSearch(Composite parent) {
		MenuItem jspMenuItem;
		MenuItem actionMenuItem;
		MenuItem allMenuItem;

		final Composite composite = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true,
				false);
		gd_composite.heightHint = 28;
		composite.setLayoutData(gd_composite);

		final Label label = new Label(composite, SWT.NONE);
		label.setText(Messages.getString("FilterDialog.searchresult")); //$NON-NLS-1$

		displayLabel = new Label(composite, SWT.NONE);
		Color color = new Color(Display.getCurrent(), new RGB(255, 0, 0));
		displayLabel.setForeground(color);
		Font font = new Font(Display.getCurrent(), "", 9, SWT.NONE); //$NON-NLS-1$
		displayLabel.setFont(font);
		final GridData gd_displayLabel = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		displayLabel.setLayoutData(gd_displayLabel);

		ToolBar toolBar;
		toolBar = new ToolBar(composite, SWT.FLAT);
		final ToolItem projectToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);

		projectToolItem.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		final Menu menu = new Menu(toolBar);
		addDropDown(projectToolItem, menu);

		jspMenuItem = new MenuItem(menu, SWT.NONE);
		jspMenuItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setListElements(res[1]);
				dialogChanged(res[1]);
			}
		});
		jspMenuItem.setText(Messages.getString("FilterDialog.searchjsp")); //$NON-NLS-1$

		actionMenuItem = new MenuItem(menu, SWT.NONE);
		actionMenuItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setListElements(res[2]);
				dialogChanged(res[2]);
			}
		});
		actionMenuItem.setText(Messages.getString("FilterDialog.searchaction")); //$NON-NLS-1$

		allMenuItem = new MenuItem(menu, SWT.NONE);
		allMenuItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {

				setListElements(res[0]);
				dialogChanged(res[0]);

			}
		});
		allMenuItem.setText(Messages.getString("FilterDialog.searchall")); //$NON-NLS-1$

		return composite;
	}

	class GUIUpdater implements Runnable {
		private IProgressMonitor monitor;

		GUIUpdater(IProgressMonitor monitor) {
			this.monitor = monitor;
		}

		public void run() {
			for (int i = 0; i < 34; i++) {
				try {
					Thread.sleep(800);
					this.monitor.worked(3);
					// if (workspaceKey != null) {
					// this.monitor.worked(100);
					// break;
					// }
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}

	private static void addDropDown(final ToolItem item, final Menu menu) {
		item.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.ARROW) {
					Rectangle rect = item.getBounds();
					Point pt = new Point(rect.x, rect.y + rect.height);
					pt = item.getParent().toDisplay(pt);
					menu.setLocation(pt.x, pt.y);
					menu.setVisible(true);
				}
			}
		});
	}

	private void dialogChanged(String[] aa) {
		// if (aa.length == 0 || aa[0] == null) {
		if (aa.length == 0) {
			displayLabel.setText(Messages.getString("FilterDialog.withoutsearchresult")); //$NON-NLS-1$
		} else {
			displayLabel.setText(""); //$NON-NLS-1$
		}
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("FilterDialog.search")); //$NON-NLS-1$
	}

}
